#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: data_generate_base.py 
@version:
@time: 2022/03/16 
"""
from TapClientCore.T01_init_conf.v2_init.init_config import MFA, parallel_record_collection
from TapClientCaseData.ColumnGenerateData.dynamic.base.base00_common import *
from TapClientConnector import TapClientConnector
from db_info import *
from TapClientCaseData.ColumnGenerateData.dynamic.base import base00_common


class DataGenerateBase:
    TEMPLATE = {
        'name': '',
        'table': '',
        'is_use': '',
        'db_name': '',
    }
    TEMPLATE2 = {
        'name': '',
        'table': [],
        'is_use': '',
        'db_name': '',
    }

    def insert_template(self, data):
        test_table_type = data.get('test_table_type')
        db_name = data.get('db_info').get('name')
        res = MFA.query(parallel_record_collection, {'name': test_table_type, 'db_name': db_name})
        if res:
            for i in res:
                _table = i.get('table')
                self._insert_data(data, table=_table)

    @staticmethod
    def _insert_data(data, table):
        db_info = data.get('db_info')
        create_column = data.get('create')
        insert_column = data.get('insert')
        _source = TapClientConnector(db_info)
        insert = _source.tapclient_connector.generate_insert(insert=insert_column, table=table)
        exec_func = _source.tapclient_connector.exec
        _source.tapclient_connector.exec(insert)

    def reset_table_template(self, data):
        """
        将表恢复初始状态
        :param data:
        :return:
        """
        test_table_type = data.get('test_table_type')
        db_name = data.get('db_info').get('name')
        res = MFA.query(parallel_record_collection, {'name': test_table_type, 'db_name': db_name})
        if res:
            for i in res:
                _table = i.get('table')
                self._reset_table(data, table=_table)
                MFA.update(parallel_record_collection, {'_id': res.get('_id')}, {'$set': {'is_use': '0'}})

    @staticmethod
    def _reset_table(data, table):
        db_info = data.get('db_info')
        where_str = data.get('where')
        _source = TapClientConnector(db_info)
        delete = _source.tapclient_connector.generate_delete(table=table, where=where_str)
        _source.tapclient_connector.exec(delete)

    async def create_template(self, data):
        test_table_type = data.get('test_table_type')
        for one in range(1, data.get('amount') + 1):
            tmp = self.TEMPLATE.copy()
            tmp['name'] = test_table_type
            tmp['is_use'] = '0'
            tmp['table'] = await self._create_data(data)
            tmp['db_name'] = data.get('db_info').get('name')
            MFA.insert_one(parallel_record_collection, tmp)

    @staticmethod
    async def _create_data(data):
        table = data.get('table')
        db_info = data.get('db_info')
        create_column = data.get('create')
        insert_column = data.get('insert')
        insert_number = data.get('insertNumber')
        _source = TapClientConnector(db_info)
        if table:
            table += _source.tapclient_connector.salt
        else:
            table = _source.tapclient_connector.CURRENT_TABLE
        table = table.lower()
        create = _source.tapclient_connector.generate_create(table_column=create_column, table=table)

        exec_func = _source.tapclient_connector.exec
        if insert_number > 1:
            if _source.database_type.lower() in ['mongodb', 'kafka']:
                for number in range(1, insert_number + 1):
                    create_column[0].update({'pk': number, '_id': table + str(number)})
                    insert = _source.tapclient_connector.generate_insert(insert_list=create_column, table=table)
                    _source.tapclient_connector.exec(insert)
            else:
                _source.tapclient_connector.exec(create)
                for number in range(1, insert_number + 1):
                    new = insert_column[0].replace('1', str(number))
                    insert = _source.tapclient_connector.generate_insert(insert=[new], table=table)
                    _source.tapclient_connector.exec(insert)
        else:
            if _source.database_type.lower() in ['kafka']:
                _source.tapclient_connector.exec(create)
            else:
                insert = _source.tapclient_connector.generate_insert(insert_column)
                _source.tapclient_connector.execMany(
                    exec_func,
                    [
                        create,
                        insert,
                    ]
                )
        return table

    async def create_more_table_template(self, data):
        test_table_type = data.get('test_table_type')
        tmp = {
            'name': '',
            'table': [],
            'is_use': '',
            'db_name': '',
        }
        for one in range(1, data.get('amount') + 1):
            tmp['table'].append(await self._create_data(data))
        tmp['db_name'] = data.get('db_info').get('name')
        tmp['name'] = test_table_type
        tmp['is_use'] = '0'
        MFA.insert_one(parallel_record_collection, tmp)
